﻿//准备ExtJS环境
Ext.require
(
    [
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.state.*',
        'Ext.chart.*',
        'Ext.form.*'
    ]
);

//组织结构数据存储
var store_organization;

//检索窗体
var win_search = null;

//定义用户表格控件
Ext.define
(
    'NewlyReport.Grid',
    {
        extend: 'Ext.grid.Panel',

        //检索窗体
        win_search: null,

        //编辑窗体
        win_edit: null,

        //新增窗体
        win_create: null,

        //初始化组件
        initComponent: function ()
        {
            Ext.apply
            (
                this,
                {
                    listeners:
                    {
                        //设置两个指头的滚动
                        render: Common.ScrollableRender
                    },
                    dockedItems:
                    [
                        {
                            xtype: 'toolbar',
                            height: 36,
                            items:
                            [
                                {
                                    iconCls: 'icon-refresh',
                                    text: '刷新',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '刷新数据',
                                    handler: this.onRefreshClick
                                },
                                '-',
                                {
                                    iconCls: 'icon-search',
                                    text: '检索',
                                    itemId: 'account-search',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '检索用户',
                                    handler: this.onSearchClick
                                }
                            ]
                        }
                    ]
                }
            );
            this.callParent();
            this.getSelectionModel().on('selectionchange', this.onSelectionChange, this);
        },

        //处理选中项目变化事件
        onSelectionChange: function (selectionModel, selections)
        {
        },

        //处理刷新按钮点击事件
        onRefreshClick: function ()
        {
            //获得表格控件
            var grid = Ext.getCmp('single_station_grid');

            //判断控件是否有效
            if (grid && grid.store.filters.length > 0)
            {
                //刷新数据
                grid.store.load();
            }
        },

        //处理检索按钮点击事件
        onSearchClick: function ()
        {
            //获得表格控件
            var grid = Ext.getCmp('single_station_grid');

            //获得数据存储对象
            var store = grid.store;

            //判定窗体是否已经创建
            if (!win_search)
            {
                //创建窗体
                win_search = Ext.create
                (
                    'Ext.window.Window',
                    {
                        title: '检索数据',
                        maximizable: false,
                        maximized: false,
                        width: 360,
                        height: 240,
                        autoScroll: false,
                        bodyBorder: false,
                        draggable: true,
                        isTopContainer: true,
                        modal: true,
                        resizable: false,
                        bodyPadding: 0,
                        closeAction: 'hide',
                        layout: 'fit',
                        defaultFocus: 'cboStation',
                        items:
                        [
                            {
                                xtype: 'form',
                                frame: false,
                                bodyPadding: 12,
                                bodyStyle: 'background-color:White;margin:0;',
                                bodyBorder: false,
                                border: 0,
                                defaultType: 'textfield',
                                fieldDefaults:
                                {
                                    labelAlign: 'left',
                                    labelWidth: 72,
                                    anchor: '100%'
                                },
                                items:
                                [
                                    {
                                        xtype: 'combobox',
                                        name: 'unit_code',
                                        itemId: 'cboOrganization',
                                        fieldLabel: '所属单位',
                                        emptyText: '所属单位',
                                        store: store_master_organization,
                                        displayField: 'Name',
                                        valueField: 'ID',
                                        queryMode: 'local',
                                        hideTrigger: false,
                                        forceSelection: true,
                                        typeAhead: true,
                                        autocomplete: true,
                                        allowBlank: false
                                    }
                                ],
                                buttons:
                                [
                                    {
                                        text: '确定',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //获得表单对象
                                            var form = this.up('form');

                                            //检查数据是否合法
                                            if (form.getForm().isValid())
                                            {
                                                //隐藏窗体
                                                this.up('window').hide();

                                                //清除过滤条件
                                                store.filters.clear();

                                                //过滤数据
                                                store.filter
                                                (
                                                    [
                                                        {
                                                            property: 'unit_code',
                                                            value: form.down('#cboOrganization').getValue()
                                                        }
                                                    ]
                                                );
                                            }
                                        }
                                    },
                                    {
                                        text: '取消',
                                        height: 26,
                                        margin: 3,
                                        handler: function ()
                                        {
                                            //隐藏窗体
                                            this.up('window').hide();
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                );
            }

            //显示窗体
            win_search.show();
        }
    }
);

//挂接环境准备完毕事件
Ext.onReady
(
    function ()
    {
        //初始化快速提示
        Ext.QuickTips.init();

        //设置状态保存提供者（当前设置为Cookie）
        Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider', { path: _Current_ActionUrl }));

        //初始化附加验证类型
        Common.InitAdditionalValidation();

        //创建单位类型存储存储对象
        store_master_organization = Ext.create
        (
            'Ext.data.Store',
            {
                fields: ['ID', 'Name'],
                data: master_organization
            }
        );

        //创建数据存储对象
        var store = Ext.create
        (
            'Ext.data.Store',
            {
                pageSize: 1000,
                autoSync: false,
                autoLoad: false,
                root: 'data',
                remoteFilter: true,
                remoteSort: true,
                model: 'StationWaterListRow',
                sorters:
                [
                    {
                        property: 'st_code',
                        direction: 'ASC'
                    }
                ],
                proxy:
                {
                    type: 'ajax',
                    batchActions: false,
                    url: Common.MakeAbsoluteUrl('Select'),
                    reader:
                    {
                        type: 'json',
                        root: 'data'
                    },
                    listeners:
                    {
                        exception: function (proxy, response, operation, options)
                        {
                            //处理错误
                            Common.HandleProxyError(proxy, response, operation, options);
                        }
                    }
                }
            }
        );

        //创建表格
        var grid = Ext.create
        (
            'NewlyReport.Grid',
            {
                store: store,
                stateful: true,
                stateId: 'single_station_grid_state',
                height: 600,
                width: '100%',
                id: 'single_station_grid',
                renderTo: 'data-grid',
                title: '',
                columnLines: true,
                listeners:
                {
                    //设置两个指头的滚动
                    render: Common.ScrollableRender
                },
                viewConfig:
                {
                    stripeRows: true,
                    loadingText: '数据加载中...',
                    singleSelect: true
                },
                //创建分页工具栏
                bbar: Ext.create
                (
                    'Ext.PagingToolbar',
                    {
                        store: store,
                        displayInfo: true,
                        displayMsg: '第 {0} - 第 {1} 条记录，共 {2} 条记录',
                        emptyMsg: ''
                    }
                ),
                columns:
                [
                    {
                        text: '代码',
                        width: 60,
                        minWidth: 60,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'st_code',
                        hideable: false
                    },
                    {
                        text: '名称',
                        width: 320,
                        minWidth: 240,
                        sortable: true,
                        align: 'left',
                        dataIndex: 'name',
                        hideable: false
                    },
                    {
                        text: '日期',
                        width: 140,
                        minWidth: 120,
                        sortable: true,
                        align: 'left',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d H:i'),
                        dataIndex: 'dt'
                    },
                    {
                        text: '流量',
                        width: 140,
                        minWidth: 120,
                        sortable: true,
                        align: 'right',
                        dataIndex: 'w_flow',
                        renderer: Ext.util.Format.numberRenderer('0,000.00'),
                        hideable: false
                    },
                    {
                        text: '水位',
                        width: 140,
                        minWidth: 120,
                        sortable: false,
                        align: 'right',
                        dataIndex: 'w_level',
                        renderer: Ext.util.Format.numberRenderer('0,000.00'),
                        hideable: false
                    }
                ]
            }
        );

        $(window).bind
        (
            "resize",
            function ()
            {
                //调整表格宽度
                Common.AdjustPanelSize('single_station_grid', 'body_content');
            }
        );

        //调整表格宽度
        Common.AdjustPanelSize('single_station_grid', 'body_content');
    }
);
